﻿// Decompiled with JetBrains decompiler
// Type: UnityEngine.Rendering.ShaderKeyword
// Assembly: UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E6AE6B5A-2752-4B07-9528-C8B77C62CE79
// Assembly location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.dll
// XML documentation location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.xml

using System;
using System.Runtime.CompilerServices;
using UnityEngine.Bindings;
using UnityEngine.Scripting;

#nullable disable
namespace UnityEngine.Rendering
{
  /// <summary>
  ///   <para>Represents an identifier for a specific code path in a shader.</para>
  /// </summary>
  [NativeHeader("Runtime/Shaders/Keywords/KeywordSpaceScriptBindings.h")]
  [NativeHeader("Runtime/Graphics/ShaderScriptBindings.h")]
  [UsedByNativeCode]
  public struct ShaderKeyword
  {
    internal string m_Name;
    internal uint m_Index;
    internal bool m_IsLocal;
    internal bool m_IsCompute;
    internal bool m_IsValid;

    [FreeFunction("ShaderScripting::GetGlobalKeywordCount")]
    [MethodImpl(MethodImplOptions.InternalCall)]
    internal static extern uint GetGlobalKeywordCount();

    [FreeFunction("ShaderScripting::GetGlobalKeywordIndex")]
    [MethodImpl(MethodImplOptions.InternalCall)]
    internal static extern uint GetGlobalKeywordIndex(string keyword);

    [FreeFunction("ShaderScripting::GetKeywordCount")]
    [MethodImpl(MethodImplOptions.InternalCall)]
    internal static extern uint GetKeywordCount(Shader shader);

    [FreeFunction("ShaderScripting::GetKeywordIndex")]
    [MethodImpl(MethodImplOptions.InternalCall)]
    internal static extern uint GetKeywordIndex(Shader shader, string keyword);

    [FreeFunction("ShaderScripting::GetKeywordCount")]
    [MethodImpl(MethodImplOptions.InternalCall)]
    internal static extern uint GetComputeShaderKeywordCount(ComputeShader shader);

    [FreeFunction("ShaderScripting::GetKeywordIndex")]
    [MethodImpl(MethodImplOptions.InternalCall)]
    internal static extern uint GetComputeShaderKeywordIndex(ComputeShader shader, string keyword);

    [FreeFunction("ShaderScripting::CreateGlobalKeyword")]
    [MethodImpl(MethodImplOptions.InternalCall)]
    internal static extern void CreateGlobalKeyword(string keyword);

    [FreeFunction("ShaderScripting::GetKeywordType")]
    [MethodImpl(MethodImplOptions.InternalCall)]
    internal static extern ShaderKeywordType GetGlobalShaderKeywordType(uint keyword);

    /// <summary>
    ///   <para>The name of the shader keyword. (Read Only)</para>
    /// </summary>
    public string name => this.m_Name;

    /// <summary>
    ///   <para>Returns the type of global keyword: built-in or user defined.</para>
    /// </summary>
    /// <param name="index"></param>
    public static ShaderKeywordType GetGlobalKeywordType(ShaderKeyword index)
    {
      return index.IsValid() ? ShaderKeyword.GetGlobalShaderKeywordType(index.m_Index) : ShaderKeywordType.UserDefined;
    }

    /// <summary>
    ///   <para>Initializes a new instance of the ShaderKeyword class from a shader global keyword name.</para>
    /// </summary>
    /// <param name="keywordName">The name of the keyword.</param>
    public ShaderKeyword(string keywordName)
    {
      this.m_Name = keywordName;
      this.m_Index = ShaderKeyword.GetGlobalKeywordIndex(keywordName);
      if (this.m_Index >= ShaderKeyword.GetGlobalKeywordCount())
      {
        ShaderKeyword.CreateGlobalKeyword(keywordName);
        this.m_Index = ShaderKeyword.GetGlobalKeywordIndex(keywordName);
      }
      this.m_IsValid = true;
      this.m_IsLocal = false;
      this.m_IsCompute = false;
    }

    /// <summary>
    ///   <para>Initializes a new instance of the ShaderKeyword class from a local shader keyword name.</para>
    /// </summary>
    /// <param name="shader">The shader that declares the keyword.</param>
    /// <param name="keywordName">The name of the keyword.</param>
    public ShaderKeyword(Shader shader, string keywordName)
    {
      this.m_Name = keywordName;
      this.m_Index = ShaderKeyword.GetKeywordIndex(shader, keywordName);
      this.m_IsValid = this.m_Index < ShaderKeyword.GetKeywordCount(shader);
      this.m_IsLocal = true;
      this.m_IsCompute = false;
    }

    /// <summary>
    ///   <para>Initializes a new instance of the ShaderKeyword class from a local shader keyword name, and the compute shader that defines that local keyword.</para>
    /// </summary>
    /// <param name="shader">The compute shader that declares the local keyword.</param>
    /// <param name="keywordName">The name of the keyword.</param>
    public ShaderKeyword(ComputeShader shader, string keywordName)
    {
      this.m_Name = keywordName;
      this.m_Index = ShaderKeyword.GetComputeShaderKeywordIndex(shader, keywordName);
      this.m_IsValid = this.m_Index < ShaderKeyword.GetComputeShaderKeywordCount(shader);
      this.m_IsLocal = true;
      this.m_IsCompute = true;
    }

    /// <summary>
    ///   <para>Returns true if the keyword is local.</para>
    /// </summary>
    /// <param name="keyword"></param>
    public static bool IsKeywordLocal(ShaderKeyword keyword) => keyword.m_IsLocal;

    /// <summary>
    ///   <para>Checks whether the global shader keyword exists.</para>
    /// </summary>
    /// <returns>
    ///   <para>Returns true if the global shader keyword exists. Otherwise, returns false.</para>
    /// </returns>
    public bool IsValid() => this.m_IsValid;

    /// <summary>
    ///   <para>Checks whether the shader keyword exists in the compute shader you pass in.</para>
    /// </summary>
    /// <param name="shader">The shader that declares the keyword.</param>
    /// <returns>
    ///   <para>Returns true if the shader keyword exists. Otherwise, returns false.</para>
    /// </returns>
    public bool IsValid(ComputeShader shader) => this.m_IsValid;

    /// <summary>
    ///   <para>Checks whether the shader keyword exists in the shader you pass in.</para>
    /// </summary>
    /// <param name="shader">The shader that declares the keyword.</param>
    /// <returns>
    ///   <para>Returns true if the shader keyword exists. Otherwise, returns false.</para>
    /// </returns>
    public bool IsValid(Shader shader) => this.m_IsValid;

    /// <summary>
    ///   <para>The index of the shader keyword.</para>
    /// </summary>
    public int index => (int) this.m_Index;

    public override string ToString() => this.m_Name;

    /// <summary>
    ///   <para>Gets the type of the keyword.</para>
    /// </summary>
    /// <param name="shader"></param>
    /// <param name="index"></param>
    [Obsolete("GetKeywordType is deprecated. Only global keywords can have a type. This method always returns ShaderKeywordType.UserDefined.")]
    public static ShaderKeywordType GetKeywordType(Shader shader, ShaderKeyword index)
    {
      return ShaderKeywordType.UserDefined;
    }

    /// <summary>
    ///   <para>Gets the type of the keyword.</para>
    /// </summary>
    /// <param name="shader"></param>
    /// <param name="index"></param>
    [Obsolete("GetKeywordType is deprecated. Only global keywords can have a type. This method always returns ShaderKeywordType.UserDefined.")]
    public static ShaderKeywordType GetKeywordType(ComputeShader shader, ShaderKeyword index)
    {
      return ShaderKeywordType.UserDefined;
    }

    /// <summary>
    ///   <para>Gets the string name of the global keyword.</para>
    /// </summary>
    /// <param name="index"></param>
    [Obsolete("GetGlobalKeywordName is deprecated. Use the ShaderKeyword.name property instead.")]
    public static string GetGlobalKeywordName(ShaderKeyword index) => index.m_Name;

    /// <summary>
    ///   <para>Gets the string name of the keyword.</para>
    /// </summary>
    /// <param name="shader"></param>
    /// <param name="index"></param>
    [Obsolete("GetKeywordName is deprecated. Use the ShaderKeyword.name property instead.")]
    public static string GetKeywordName(Shader shader, ShaderKeyword index) => index.m_Name;

    /// <summary>
    ///   <para>Gets the string name of the keyword.</para>
    /// </summary>
    /// <param name="shader"></param>
    /// <param name="index"></param>
    [Obsolete("GetKeywordName is deprecated. Use the ShaderKeyword.name property instead.")]
    public static string GetKeywordName(ComputeShader shader, ShaderKeyword index) => index.m_Name;

    /// <summary>
    ///   <para>Gets the type of the keyword.</para>
    /// </summary>
    /// <param name="shader"></param>
    /// <param name="index"></param>
    [Obsolete("GetKeywordType is deprecated. Use ShaderKeyword.name instead.")]
    public ShaderKeywordType GetKeywordType() => ShaderKeyword.GetGlobalKeywordType(this);

    /// <summary>
    ///   <para>Gets the string name of the keyword.</para>
    /// </summary>
    /// <param name="shader"></param>
    /// <param name="index"></param>
    [Obsolete("GetKeywordName is deprecated. Use ShaderKeyword.name instead.")]
    public string GetKeywordName() => ShaderKeyword.GetGlobalKeywordName(this);

    [Obsolete("GetName() has been deprecated. Use ShaderKeyword.name instead.")]
    public string GetName() => this.GetKeywordName();
  }
}
